package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.table.JobseekerAssessTO;
import com.xingkeduo.pojo.table.JobseekerAssessTO.Result;
import com.xingkeduo.pojo.table.JobseekerAssessTO.Type;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 求职者考核结果封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:45:17
 * @description
 */
@Component
public class JobseekerAssessJTO extends BaseJson {
    public static JobseekerAssessTO createTableForNew(String json) throws Throwable {
        JobseekerAssessTO table = new JobseekerAssessTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setType(validType(root, YesNoEnum.YES));
        table.setResult(validResult(root, YesNoEnum.YES));
        if (table.getType() == Type.TEST1 && table.getResult() == Result.PASS)
            table.setEnterTime(validEnterTime(root, YesNoEnum.YES));
        table.setComments(validComments(root, YesNoEnum.NO)).setLogin(validLogin(root, YesNoEnum.NO));

        return table;
    }

    public static ObjectNode toJson(JobseekerAssessTO table) {
        ObjectNode root = MAPPER.createObjectNode();
        SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (table != null) {
            if (table.getType() != null) {
                root.put("type_value", table.getType().getValue());
                root.put("type_desc", table.getType().getDesc());
            }
            if (table.getResult() != null) {
                root.put("result_value", table.getResult().getValue());
                root.put("result_desc", table.getResult().getDesc());
            }
            if (table.getEnterTime() != null)
                root.put("entertime", formatTimestamp.format(table.getEnterTime()));
            if (table.getComments() != null)
                root.put("comments", table.getComments());
            if (table.getLogin() != null)
                root.put("login", table.getLogin());
            if (table.getCreatedTime() != null)
                root.put("createdtime", formatTimestamp.format(table.getCreatedTime()));
            if (!CommonUtil.isEmpty(table.getRates()))
                root.putPOJO("rates", JobseekerAssessRateJTO.toJson(table.getRates()));

            toBaseJson(root, table);
        }

        return root;
    }

    public static ArrayNode toJson(List<JobseekerAssessTO> tables) {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<JobseekerAssessTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }

    public static int validScore1(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "score1";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isPositiveInteger(value) || Integer.parseInt(value) > 5)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Integer.parseInt(value);
        }
        return 0;
    }

    public static int validScore2(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "score2";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isPositiveInteger(value) || Integer.parseInt(value) > 5)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Integer.parseInt(value);
        }
        return 0;
    }

    public static Type validType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "type";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!Type.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Type.getType(value);
        }
        return null;
    }

    public static Result validResult(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "result";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!Result.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Result.getResult(value);
        }
        return null;
    }

    public static Date validEnterTime(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "entertime";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isDateTime(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(value);
        }
        return null;
    }

    public static String validComments(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "comments";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 100)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validLogin(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "login";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }
}
